<?php 
/**
 *猜拳静态类
 */
class App_mora{
	/**
	 * 获取猜拳的收入榜
	 * @param <int> $uin
	 * @param <boolean> $insert 是否在不存在时，新写入数据
	 */
	public static function getGain($uin , $insert = true) {
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_mora_gain` WHERE `uin` = " . $uin);
		
		// 如果金库数据不存在，则新写入一条数据
		if($insert && $result === false) {
			$query_data = array(
				'uin' => $uin,
				'gain' => 0,
				'total' => 0,
				'win' => 0,
				'rate' => 0,
				'time' => 0,
			);
			$Db->doInsert('app_mora_gain' , $query_data);
			
			$result = $query_data;
		}
		
		return $result;
	}
	/**
	 * 获取单个用户称号
	 * Enter description here ...
	 */
	public static function getRank($uin , $gain = false) {
		// 如果没有传进来数据，则从数据库获取
		if($gain === false) {
			$result = self::getGain($uin);
			
			if(empty($result)) $gain = 0;
			else $gain = $result['gain'];
		}
		if($gain >= 2000000) {
			return array('level' => 16 , 'name' => '无人能敌');
		} else if($gain >= 500000) {
			return array('level' => 15 , 'name' => '出神入化');
		} else if($gain >= 80000) {
			return array('level' => 14 , 'name' => '登峰造极');
		} else if($gain >= 50000) {
			return array('level' => 13 , 'name' => '功行圆满');
		} else if($gain >= 32000) {
			return array('level' => 11 , 'name' => '已臻大成');
		} else if($gain >= 15000) {
			return array('level' => 10 , 'name' => '自成一派');
		} else if($gain >= 11800) {
			return array('level' => 9 ,  'name' => '炉火纯青');
		} else if($gain >= 8000) {
			return array('level' => 8 ,  'name' => '渐入佳境');
		} else if($gain >= 4200) {
			return array('level' => 7 ,  'name' => '略有小成');
		} else if($gain >= 2400) {
			return array('level' => 6 ,  'name' => '初窥堂奥');
		} else if($gain >= 1000) {
			return array('level' => 5 ,  'name' => '圆转纯熟');
		} else if($gain >= 520) {
			return array('level' => 4 ,  'name' => '登堂入室');
		} else if($gain >= 100) {
			return array('level' => 3 ,  'name' => '初学乍练');
		} else if($gain >= 0) {
			return array('level' => 2 ,  'name' => '初来驾到');
		} else {
			return array('level' => 1 ,  'name' => '一败涂地');
		}
	}
	/**
	 * 获取排行榜总数
	 */
	public static function getTopCount(){
		$Db = loadDB();
		$result= $Db->getOne("SELECT COUNT(*) AS `count` FROM `app_mora_gain`");
		return $result;
	}
	/**
	 * 获取排行榜信息
	 */
	public static function getTopList($type , $pageSize, $pageCount){
		$Db = loadDB();
		$pageSize=($pageSize-1)*$pageCount;
		$result = $Db->getAll("SELECT * FROM `app_mora_gain` ORDER BY `{$type}` DESC LIMIT {$pageSize} , {$pageCount}");
		return $result;
	}
	/**
	 * 获取多个用户称号
	 * @param <array> $uins
	 * @param <array> $gains
	 */
	public static function getRanks($uins , $gains = false) {
		if(empty($gains)) {
			$uins_str = implode(',' , $uins);
			
			$Db = loadDB();
			$result = $Db->getAll("SELECT `uin` , `gain` FROM `app_mora_gain` WHERE `uin` IN '{$uins_str}'");
			
			$gains = array();
			if(!empty($result)) {
				foreach($result as $row) {
					$gains[$row['uin']] = $row['gain'];
				}
			}
		}
		
		$return_arr = array();
		foreach($uins as $uin) {
			$gain = empty($gains[$uin]) ? 0 : $gains[$uin];
			$return_arr[$uin] = self::getRank($uin , $gain);
		}
		
		return $return_arr;
	}
	/**
	 * 存储当前猜拳
	 * @param <int> $uin
	 * @param <int> $id 传入了则为修改，否则为新增
	 * @param <int> $turn 0 => 主人 , 1 => 客人
	 * @param <array> $owner_cards	主人的牌
	 * @param <array> $guest_cards	客人的牌
	 * @param <int> $stake	赌注
	 */
	public static function recordState($uin , &$id , $guest_uin , $turn , $owner_cards ,  $stake) {
		$Db = loadDB();
		
		// 按格式打包，暂时是用 serialize
		$state[] = array(
			'owner_cards' => $owner_cards,
		);
		$state = serialize($state);
		//print_r($state);
		$time = time();
		
		//把数据写入数组
		$query_data = array(
			'guest_uin' => $guest_uin,
			'turn' => $turn,
			'state' => $state,
			'stake' => $stake,
			'time' => $time,
		);
		
			// 新增
			
			$id = $time;		// 分配一个id
			$query_data['id'] = $id;
			$query_data['uin'] = $uin;
			
			$success = $Db->doInsert('app_mora_state' , $query_data);
		
		
		return $success ? 0 : 511;
	}
	/**
	 * 判断是否有当前猜拳状态
	 * @param <int> $uin
	 */
	public static function getMora($uin){
		$Db = loadDB();
		$result= $Db->getOne("SELECT * FROM `app_mora_state` WHERE `turn`=0 AND `uin` = " . $uin);
	
		//解析单条
		
		$result['state'] = empty($result['state']) ? array() : unserialize($result['state']);
		
		return $result;
	}
	/**
	 * 应战查询
	 * @param <int> $id
	 * @param <int> $uin
	 */
	public static function getOneMora($owner_uin , $id){
		$Db = loadDB();
		//print_r($owner_uin);
		$result= $Db->getOne("SELECT * FROM `app_mora_state` WHERE `uin` = {$owner_uin} AND `id` = {$id}");
	//print_r($result);
		//解析单条
		
		$result['state'] = empty($result['state']) ? array() : unserialize($result['state']);
		//print_r($result);
		return $result;
	}
	/**
	 * 修改猜拳
	 */
	public static function getUpMora($id , $owner_uin , $owner_cards , $guest_cards , $org_mora = false){
		$Db=loadDB();
		// 取原数据
		if(empty($org_mora)) {
			$mora = self::getMora($id);
			if(empty($mora)) {
				return 514;	// 该猜拳信息不存在
			}
		} else {
			$mora = $org_mora;
		}
		//print_r($guest_cards);
		if(!empty($owner_cards)){
			$mora['state'][] = array('owner_cards' => $owner_cards,);
		}
		
		if(!empty($guest_cards)){
			$mora['state'][] = array('guest_cards' => $guest_cards,);
		}
		//$mora['state'][] = array('owner_cards' => $owner_cards,);
		
		// 写入数据
		$update_data = array(
			'state' => serialize($mora['state']),
		);
		//print_r($)
		$success = $Db->doUpdate('app_mora_state' , $update_data , '`uin`= '.$owner_uin.' AND `id`= ' . $id);
	//print_r($mora);
		return $success ? 0 : 512;
	}
	/**
	 * 获取用户已可应战的拳局
	 * @param <int> $limit
	 */
	public static function getRandStateList() {
		$Db = loadDB();
		
		
		
		$result = $Db->getAll("SELECT * FROM `app_mora_state`,`app_mora_gain` WHERE `turn` = 1 AND app_mora_state.uin=app_mora_gain.uin  ORDER BY RAND() DESC LIMIT 10");
		if(empty($result)) return array();
		
		foreach($result as $k => $row) {
			$result[$k]['state'] = unserialize($row['state']);
		}
		
		return $result;
	}
	/**
	 * 应战拳局
	 */
	public static function getUpState($id , $owner_uin , $uin){
		$Db=loadDB();
		$update_data = array(
			'guest_uin' => $uin,
		);
		$success = $Db->doUpdate('app_mora_state' , $update_data , '`uin`= '.$owner_uin.' AND `id` = ' . $id);
		
	}
	/**
	 * 判断是否存在该局
	 * Enter description here ...
	 * @param $uin
	 * @param $id
	 */
	public static function getState($uin , $id) {
		$Db = loadDB();
		
		$result = $Db->getOne("SELECT * FROM `app_mora_state` WHERE `uin` = {$uin} AND `id` = {$id}");
		if(empty($result)) return array();
		
		$result['state'] = unserialize($result['state']);
		
		return $result;
	}
	/**
	 * addStake
	 * @param <int> $id
	 * @param <int> $stake
	 */
	public static function addstake($uin , $id , $turn , $steke){
		$Db=loadDB();
		
		$update_data = array(
			'turn' => $turn,
			'stake' => $steke,
		);
		
		$success = $Db->doUpdate('app_mora_state' , $update_data , "`uin` = {$uin} AND `id` = {$id}");
		//print_r($success);
		return $success ? 0 : 523;
	}
	/**
	 * 获取个人猜拳总量
	 * @param <int> $uin
	 */
	static public function getCount($uin) {
		$Db = loadDB();
		
		$result= $Db->getOne("SELECT COUNT(*) AS `count` From `app_mora_state` WHERE `uin`={$uin}");

		return empty($result) ? 0 : $result['count'];
	}
	/**
	 * 获取个人猜拳列表
	 * @param <int> $uin
	 * @param <int> $pageSize
	 * @param <int> $pageCount
	 */
	static public function getList($uin , $pageSize , $pageCount) {
		$Db = loadDB();
		//$pageSize=1;
		//$pageCount=10;
		$pageSize=($pageSize-1)*$pageCount;
		$list = $Db->getAll("SELECT  * FROM `app_mora_state` WHERE `uin`={$uin} ORDER BY id DESC LIMIT {$pageSize} , {$pageCount}");
		
		//解析回复
		foreach($list as $k => $row) {
			$list[$k]['state'] = empty($row['state']) ? array() : unserialize($row['state']);
		}
		
		return $list;
	}
	/**
	 * 判断输赢
	 * @param unknown_type $a
	 * @param unknown_type $b
	 * 剪刀（shears）
	 * 石头（rock）
	 * 布（cloth）
	 */
	static public function getJudge ($a , $b) {
		//print_r($b);
		if($a['owner_cards']== 'rock' && $b['guest_cards'] == 'cloth') return -1;
		if($a['owner_cards']== 'rock' && $b['guest_cards'] == 'rock') return 0;
		if($a['owner_cards']== 'rock' && $b['guest_cards'] == 'shears') return 1;
		if($a['owner_cards']== 'shears' && $b['guest_cards'] == 'cloth') return 1;
		if($a['owner_cards']== 'shears' && $b['guest_cards'] == 'rock') return -1;
		if($a['owner_cards']== 'shears' && $b['guest_cards'] == 'shears') return 0;
		if($a['owner_cards']== 'cloth' && $b['guest_cards'] == 'cloth') return 0;
		if($a['owner_cards']== 'cloth' && $b['guest_cards'] == 'rock') return 1;
		if($a['owner_cards']== 'cloth' && $b['guest_cards'] == 'shears') return -1;
	}
	/**
	 * 拳局胜利修改
	 * @param <int> $id
	 * @param <int> $steke
	 */
	public static function updeteGain($uin,$gain,$total,$win,$rate){
		$Db=loadDB();
		$time = time();
		$update_data = array(
			'gain' => $gain,
			'total' => $total,
			'win' => $win,
			'rate' => $rate,
			'time' => $time,
		);
		
		$success = $Db->doUpdate('app_mora_gain' , $update_data , '`uin` = ' . $uin);
		//print_r($success);
		return $success ? 0 : 523;
	}
	/**
	 * 删除拳局
	 * @param <int> $uin
	 * @param <int> $id
	 */
	public static function delState($uin , $id) {
		$Db = loadDB();
		
		$success = $Db->query("DELETE FROM `app_mora_state` WHERE `uin` = {$uin} AND `id` = {$id}");
		
		return $success ? 0 : 525;
	}
	/**
	 * 获取用户未完成的拳局
	 * @param <int> $limit
	 */
	public static function getRandUndoneList($uin) {
		$Db = loadDB();
		
		//$pageSize=($pageSize-1)*$pageCount;
		$result = $Db->getAll("SELECT * FROM `app_mora_state`,`app_mora_gain` WHERE `turn` = 2  AND app_mora_state.uin = app_mora_gain.uin AND app_mora_state.guest_uin = {$uin} ORDER BY RAND() DESC LIMIT 5");
		if(empty($result)) return array();
		
		foreach($result as $k => $row) {
			$result[$k]['state'] = unserialize($row['state']);
		}
		
		return $result;
	}
	/**
	 * 修改state
	 */
	public static function getUpMorastate($uin, $id, $state){
		$Db=loadDB();
		$is_state = serialize($state);
		$update_data = array(
			'state' => $is_state,
		);
		
		$success = $Db->doUpdate('app_mora_state' , $update_data , "`uin` = {$uin} AND `id` = {$id}");
		//print_r($success);
		return $success ? 0 : 523;
	} 
}
# end of script